//
//  YZMTradeConfigureModel.m
//  YZMicroStock
//
//  Created by wangminhong on 16/6/3.
//  Copyright © 2016年 cqjr. All rights reserved.
//

#import "YZMTradeConfigureModel.h"
#import "YZMSeverTimeService.h"
#import "JSONBaseDeal.h"
#import "Mantle+Utils.h"
#import "DTTimePeriod+Utils.h"
#import <DateTools/DateTools.h>
#import "NSNumber+Utils.h"
#import "YZMTradeTypeDef.h"

@interface YZMTradeConfigureModel()

@property (nonatomic, strong) NSString *risk;

@end

@implementation YZMTradeConfigureModel


+ (NSDictionary *)JSONKeyPathsByPropertyKey {
    return @{
             @"requestTime":@"nowTime",
             @"risk":@"strRisk",
             @"isSignAgreement":@"signAgreement",
             @"token":@"token",
             @"balance":@"balance",
             };
}

+ (NSValueTransformer *)requestTimeJSONTransformer
{
    return [NSValueTransformer valueTransformerForName: MTLDoubleDateTransformerName];
}

+ (NSValueTransformer *)balanceJSONTransformer
{
    return [NSValueTransformer valueTransformerForName: MTLStringDoubleTransformerName];
}

- (void)setNilValueForKey:(NSString *)key{
    
}

- (void)encodeWithCoder:(NSCoder *)coder{
    
    [coder encodeObject:self.limitTimePeriods forKey:@"limitTimePeriods"];
    [coder encodeObject:self.limitTimeString forKey:@"limitTimeString"];
    [coder encodeObject:self.showTimePeriods forKey:@"showTimePeriods"];
    [coder encodeBool:self.isAllowedTrade forKey:@"isAllowedTrade"];
    [coder encodeObject:self.versionString forKey:@"versionString"];
    [coder encodeObject:self.holidayList forKey:@"holidayList"];
    [coder encodeBool:self.isSignAgreement forKey:@"isSignAgreement"];
    [coder encodeDouble:self.profitShare forKey:@"profitShare"];
    [coder encodeDouble:self.exchangeRate forKey:@"exchangeRate"];
    [coder encodeObject:self.tradeCountList forKey:@"tradeCountList"];
    [coder encodeObject:self.quitCloseList forKey:@"quitCloseList"];
    [coder encodeObject:self.quitGainList forKey:@"quitGainList"];
    [coder encodeDouble:self.minFloatPoint forKey:@"minFloatPoint"];
    [coder encodeDouble:self.contractValue forKey:@"contractValue"];
    [coder encodeDouble:self.lossRatio forKey:@"lossRatio"];
    [coder encodeDouble:self.serviceCharge forKey:@"serviceCharge"];
    [coder encodeObject:self.contractCode forKey:@"contractCode"];
    [coder encodeBool:self.isAgreementUpdatd forKey:@"isAgreementUpdatd"];
    [coder encodeObject:self.name forKey:@"name"];
    [coder encodeInteger:self.distanceForHour forKey:@"distanceForHour"];
}
- (instancetype)initWithCoder:(NSCoder *)coder{
    
    self = [super init];
    if (self) {
        
        self.limitTimePeriods = [coder decodeObjectForKey:@"limitTimePeriods"];
        self.limitTimeString = [coder decodeObjectForKey:@"limitTimeString"];
        self.showTimePeriods = [coder decodeObjectForKey:@"showTimePeriods"];
        self.isAllowedTrade = [coder decodeBoolForKey:@"isAllowedTrade"];
        self.versionString = [coder decodeObjectForKey:@"versionString"];
        self.holidayList = [coder decodeObjectForKey:@"holidayList"];
        self.isSignAgreement = [coder decodeBoolForKey:@"isSignAgreement"];
        self.profitShare = [coder decodeDoubleForKey:@"profitShare"];
        self.exchangeRate = [coder decodeDoubleForKey:@"exchangeRate"];
        self.tradeCountList = [coder decodeObjectForKey:@"tradeCountList"];
        self.quitCloseList = [coder decodeObjectForKey:@"quitCloseList"];
        self.quitGainList = [coder decodeObjectForKey:@"quitGainList"];
        self.minFloatPoint = [coder decodeDoubleForKey:@"minFloatPoint"];
        self.contractValue = [coder decodeDoubleForKey:@"contractValue"];
        self.lossRatio = [coder decodeDoubleForKey:@"lossRatio"];
        self.serviceCharge = [coder decodeDoubleForKey:@"serviceCharge"];
        self.contractCode = [coder decodeObjectForKey:@"contractCode"];
        self.isAgreementUpdatd = [coder decodeBoolForKey:@"isAgreementUpdatd"];
        self.name = [coder decodeObjectForKey:@"name"];
        self.distanceForHour = [coder decodeIntegerForKey:@"distanceForHour"];
    }
    
    return self;
}


- (instancetype)initWithDictionary:(NSDictionary *)dictionaryValue error:(NSError **)error{
    
    self = [super initWithDictionary:dictionaryValue error:error];
    if (self == nil) return nil;
    
    
    NSString *jsonString = self.risk;
    if (jsonString.length >0) {
        
        jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\r" withString:@""];
        jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\n" withString:@""];
        jsonString = [jsonString stringByReplacingOccurrencesOfString:@"\t" withString:@""];
        
        NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSError *err;
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&err];
        if (err) return nil;
        
        /*version*/
        {
            self.versionString = [JSONBaseDeal getValue:@"version" fromData:dict];
        }
        
        /*名称*/
        {
            self.name = [JSONBaseDeal getValue:@"value" fromData:dict[@"futuresName"]];
        }
        
        /*交易时间段*/
        {
            self.limitTimeString = [JSONBaseDeal getValue:@"value" fromData:dict[@"tradingTimeLimit"]];
            self.limitTimePeriods = [self periodAnalyzeWithString:self.limitTimeString];
        }
        
        /*盈利分成*/
        {
            self.profitShare = [JSONBaseDeal getDoubleValue:@"value" fromData:dict[@"profitShare"]];
        }
        
        /*汇率*/
        {
            self.exchangeRate = [JSONBaseDeal getDoubleValue:@"value" fromData:dict[@"exchangeRate"]];
        }
        
        /*交易可配置的手数*/
        {
            NSString *string = [JSONBaseDeal getValue:@"value" fromData:dict[@"number"]];
            if (string.length > 0) {
                
                NSArray *array = [string componentsSeparatedByString:@","];
                if (array.count >0) {
                    
                    self.tradeCountList = array;
                }
            }

        }
        /*止损数组*/
        {
            NSString *string = [JSONBaseDeal getValue:@"value" fromData:dict[@"quitCloseRatio"]];
            
            NSArray *array = [string componentsSeparatedByString:@","];
            if (array.count >0) {
                
                self.quitCloseList = array;
            }
        }
        
        /*假期*/
        {
            NSString *string = [JSONBaseDeal getValue:@"value" fromData:dict[@"holiday"]];
            self.holidayList = [self setHolidaysWithString:string];
            
        }
        
        /*最小波动*/
        {
            
            self.minFloatPoint = [JSONBaseDeal getDoubleValue:@"value" fromData:dict[@"fluctuationValue"]];
        }
        
        /*停止交易标志*/
        {
            self.isAllowedTrade = [JSONBaseDeal getBoolValue:@"value" fromData:dict[@"isTrade"]];
        }
        
        /*每手点数*/
        {
            self.contractValue = [JSONBaseDeal getDoubleValue:@"value" fromData:dict[@"contractValue"]];
        }
        
        /*止损比例*/
        {
            self.lossRatio = [JSONBaseDeal getDoubleValue:@"value" fromData:dict[@"lossRatio"]];
        }
        
        /*一手的交易手续费*/
        {
            self.serviceCharge = [JSONBaseDeal getDoubleValue:@"value" fromData:dict[@"serviceCharge"]];
        }
        
        /*合约号*/
        {
            self.contractCode = [JSONBaseDeal getValue:@"value" fromData:dict[@"contractCode"]];
        }

        /*止盈线数组*/
        {
            NSString *string = [JSONBaseDeal getValue:@"value" fromData:dict[@"quitGainValue"]];
            if (string.length > 0) {
                
                NSArray *array = [string componentsSeparatedByString:@","];
                if (array.count > 0) {
                    
                    self.quitGainList = array;
                }
            }
            
        }

        self.risk = nil;
    }
    
    return self;
}

- (BOOL)changeDay{
    
    if (self.limitTimePeriods.count == 0) {
        
        return YES;
    }
    
    NSDate *date = [[YZMSeverTimeService sharedService].severDate dateBySubtractingHours:self.distanceForHour];
    DTTimePeriod *limitFirstDate = self.limitTimePeriods[0][0];
    if (date.day != limitFirstDate.StartDate.day) {
        
        self.limitTimePeriods = [self periodAnalyzeWithString:self.limitTimeString];
        return YES;
    }
    
    return NO;
}

- (NSArray *)setHolidaysWithString:(NSString *)string{
    
    /*假期字符串,日期之间用，分隔 2016-01-01,2016-02-06*/
    
    if (string.length == 0) {
        
        return nil;
    }
    
    NSArray *temp = [string componentsSeparatedByString:@","];
    if (temp.count == 0) {
        
        return nil;
    }
    
    NSMutableArray *array  = [NSMutableArray array];
    for (NSString *day in temp) {
        
        NSDate *holiday = [NSDate dateWithString:day formatString:@"yyyy-MM-dd"];
        if (holiday) {
            
            [array addObject:holiday];
        }
    }
    
    if (array.count != 0) {
        
        return array;
    }
    
    return nil;
}

- (NSArray *)currentTimePeriod{
    
    //恒指 计算显示的时间轴数据
    if (self.showTimePeriods.count == 0) {
        
        return nil;
    }
    
    if ([self isInHolidayOrWeekendWithDate:[YZMSeverTimeService sharedService].severDate]) {
        
        return [self.showTimePeriods lastObject];
    }
    
    //比当天的最早交易时间早
    DTTimePeriod *period = self.limitTimePeriods[0][0];
    if ([[YZMSeverTimeService sharedService].severDate isEarlierThan:period.StartDate]) {
        
        return [self.showTimePeriods lastObject];
    }

    
    for (NSArray *array  in self.showTimePeriods) {
        
        for (DTTimePeriod *period in array) {
            
            if ([period containsDate:[YZMSeverTimeService sharedService].severDate interval:DTTimePeriodIntervalOpen]) {
                
                return array;
            }
        }
    }
    
    //不在交易时间段内
    NSArray *subArray;
    for (NSArray *array  in self.showTimePeriods) {
        
        NSDate *startDate = [(DTTimePeriod *)[array firstObject] StartDate];
        NSDate *endDate = [(DTTimePeriod *)[array lastObject] EndDate];
        DTTimePeriod *longPeriod = [[DTTimePeriod alloc]initWithStartDate:startDate endDate:endDate];
        
        if ([longPeriod containsDate:[YZMSeverTimeService sharedService].severDate interval:DTTimePeriodIntervalOpen]) {
            
            return array;
        }
        
        if ([longPeriod.StartDate isLaterThan:[YZMSeverTimeService sharedService].severDate]) {
            
            return subArray;
        }
        
        subArray = array;
    }
    
    return subArray;
}

//YZM_TRADE_TIME_TYPE
- (NSArray *)recommandForTradeTime{
    
    if (!self.isAllowedTrade) {
        
        return [NSArray arrayWithObject:@(YZM_TRADE_TIME_TYPE_FOR_STOP)];
    }
    
    NSDate *severDate = [YZMSeverTimeService sharedService].severDate;
    
    if ([self isInHolidayOrWeekendWithDate:severDate]) {
        
        DTTimePeriod *period = self.limitTimePeriods[0][0];
        
        return [NSArray arrayWithObjects:@(YZM_TRADE_TIME_TYPE_FOR_OTHER_DAY),[[self nextTradeDayWithSeverDate:severDate] stringByAppendingString:[period.StartDate formattedDateWithFormat:@"HH:mm"]], nil];
    }
    
    for (NSArray *array  in self.limitTimePeriods) {
        
        for (DTTimePeriod *period in array) {
            
            if ([period containsDate:severDate interval:DTTimePeriodIntervalOpen]) {
                
                DTTimePeriod *lastPeriod = [array lastObject];
                return [NSArray arrayWithObjects:@(YZM_TRADE_TIME_TYPE_FOR_IN), [lastPeriod.EndDate formattedDateWithFormat:@" HH:mm"], nil];
            }
        }
    }
    
    for (NSArray *period in self.limitTimePeriods) {
        
        for (int i= 0; i< period.count; i++) {
            
            DTTimePeriod *subPeriod = period[i];
            if ([subPeriod.StartDate isLaterThan:severDate]) {
                
                //可以根据 subPeriod 判断 是否同一个交易时段内。 index >=1 YZM_TRADE_TIME_TYPE_FOR_OUT else YZM_TRADE_TIME_TYPE_FOR_OUT_TODAY
                return [NSArray arrayWithObjects:@(YZM_TRADE_TIME_TYPE_FOR_OUT_TODAY), [@"今天" stringByAppendingString:[subPeriod.StartDate formattedDateWithFormat:@" HH:mm"]], nil];
            }
        }
    }
    
    //今天休市后，下一个交易时间
    DTTimePeriod *period = self.limitTimePeriods[0][0];
    return [NSArray arrayWithObjects:@(YZM_TRADE_TIME_TYPE_FOR_OTHER_DAY), [[self nextTradeDayWithSeverDate:severDate] stringByAppendingString:[period.StartDate formattedDateWithFormat:@"HH:mm"]], nil];
}

- (NSString *)formatPrice:(double)price{
    return nil;
}

- (NSArray *)periodAnalyzeWithString:(NSString *)string{
    
    NSArray *outerArray = [string componentsSeparatedByString:@";"];
    if (outerArray.count == 0) {
        
        return nil;
    }
    NSMutableArray *outerPeriodArray = [NSMutableArray array];
    for (NSString *outerString in outerArray) {
        
        NSArray *periodArray = [outerString componentsSeparatedByString:@"|"];
        NSMutableArray *timePeriodArray = [NSMutableArray array];
        NSString *preHour;
        for (NSString *subString in periodArray) {
            
            NSArray *periodSubArray = [subString componentsSeparatedByString:@","];
            if (periodSubArray.count < 2) continue;
            
            NSDate *useDate = [[YZMSeverTimeService sharedService].severDate dateBySubtractingHours:self.distanceForHour];
            NSArray *firstSeparate = [periodSubArray[0] componentsSeparatedByString:@":"];
            NSString *hour = firstSeparate[0];
            NSString *minute = firstSeparate[1];
            
            NSArray *secondSeparate = [periodSubArray[1] componentsSeparatedByString:@":"];
            NSString *endHour = secondSeparate[0];
            NSString *endMinute = secondSeparate[1];
            
            if (preHour.length != 0) {
                
                if (hour.integerValue < preHour.integerValue) {
                    useDate = [useDate dateByAddingDays:1];
                }
            }
            
            preHour = hour;
            
            NSInteger year = [[useDate formattedDateWithFormat:@"yyyy"] integerValue];
            NSInteger month = [[useDate formattedDateWithFormat:@"MM"] integerValue];
            NSInteger day = [[useDate formattedDateWithFormat:@"dd"] integerValue];
            NSDate *beginDate = [NSDate dateWithYear:year month:month day:day hour:[hour integerValue] minute:[minute integerValue]second:0];
            if ([endHour integerValue] == 24 && [endMinute integerValue] == 0) {
                endHour = @"23";
                endMinute = @"59";
            }
            NSDate *endDate = [NSDate dateWithYear:year month:month day:day hour:[endHour integerValue] minute:[endMinute integerValue]second:0];
            
            DTTimePeriod *timePeriod = [DTTimePeriod timePeriodWithStartDate:beginDate endDate:endDate];
            
            if (timePeriod) {
                
                if (timePeriodArray.count != 0) {
                    
                    DTTimePeriod *lastPeriod = [timePeriodArray lastObject];
                    
                    if( floor([timePeriod.StartDate minutesFrom:lastPeriod.EndDate]) <=2){
                        lastPeriod.EndDate = timePeriod.EndDate;
                        [timePeriodArray removeLastObject];
                        [timePeriodArray addObject:lastPeriod];
                    }else{
                        
                        [timePeriodArray addObject:timePeriod];
                    }
                }else{
                    
                    [timePeriodArray addObject:timePeriod];
                }
                
            }
        }
        
        [outerPeriodArray addObject:timePeriodArray];
    }
    
    if (outerPeriodArray.count == 0) {
        
        return nil;
    }
    
    return outerPeriodArray;
}

- (BOOL)isInHolidayWithDate:(NSDate *)date{
    
    return YES;
}

- (BOOL)isInWeekendWithDate:(NSDate *)date{
    
    return YES;
}

- (BOOL)isInHolidayOrWeekendWithDate:(NSDate *)date{
    
    if ([self isInWeekendWithDate:date]) {
        
        return YES;
    }
    
    if ([self isInHolidayWithDate:date]) {
        
        return YES;
    }
    
    return NO;
}

- (NSArray *)getCurrentDatePeriodList{
    
    return nil;
}

- (NSString *)nextTradeDayWithSeverDate:(NSDate *)severDay{
    
    NSDate *nextDay = [severDay dateByAddingDays:1];
    NSInteger count = 1;
    while ( [self isInHolidayOrWeekendWithDate:nextDay] ) {
        
        nextDay = [nextDay dateByAddingDays:1];
        count += 1;
    }
    
    if (count == 1 && severDay.hour < self.distanceForHour) {
        
        return @"今天";
    }
    
    if (count == 1 || [[severDay dateByAddingDays:1] isSameDay:[nextDay dateBySubtractingHours:self.distanceForHour]]) {
        return @"明天";
    }
    
    return [[nextDay dateBySubtractingHours:self.distanceForHour] formattedDateWithFormat:@"MM月dd日"];
}

- (BOOL)isInShowTimePeriods{
    
    //非交易日
    if ([self isInHolidayOrWeekendWithDate:[YZMSeverTimeService sharedService].severDate]) {
        
        return NO;
    }
    
    if (self.showTimePeriods.count == 0) {
        
        return YES;
    }
    
    for (NSArray *array  in self.showTimePeriods) {
        
        for (DTTimePeriod *period in array) {
            
            if ([period containsDate:[YZMSeverTimeService sharedService].severDate interval:DTTimePeriodIntervalOpen]) {
                
                return YES;
            }
        }
    }
    
    return NO;
}

- (BOOL)isInTradeTimePeriods{
    
    //非交易日
    if ([self isInHolidayOrWeekendWithDate:[YZMSeverTimeService sharedService].severDate]) {
        
        return NO;
    }
    
    if (self.limitTimePeriods.count == 0) {
        
        return YES;
    }
    
    for (NSArray *array  in self.limitTimePeriods) {
        
        for (DTTimePeriod *period in array) {
            
            if ([period containsDate:[YZMSeverTimeService sharedService].severDate interval:DTTimePeriodIntervalOpen]) {
                
                return YES;
            }
        }
    }
    
    return NO;
}

- (void)createEncoder{
    
    [self createEncoderWithIdentify:nil];
}

- (void)createEncoderWithIdentify:(NSString *)identify{
    
    if (identify.length == 0) {
        return;
    }
    
    NSMutableData *data = [NSMutableData data];
    //创建一个归档类
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
    [archiver encodeObject:self forKey:identify];
    [archiver finishEncoding];
    //将数据写入文件里
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:identify];
    [[NSUserDefaults standardUserDefaults] synchronize];
}

+ (instancetype)createDecoderWithIdentify:(NSString *)identify{
    
    if (identify.length == 0) {
        return nil;
    }
    
    NSData *data = [[NSUserDefaults standardUserDefaults] objectForKey:identify];
    
    if (data && data.length != 0) {
        
        NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
        
        id model = [unarchiver decodeObjectForKey:identify];
        [unarchiver finishDecoding];
        
        return model;
    }
    
    return nil;
    
}

+ (instancetype)createDecoder{
    
    return [self createDecoderWithIdentify:nil];
}

- (instancetype)copyWithZone:(NSZone *)zone{
    
    YZMTradeConfigureModel *copy = [[[self class] allocWithZone:zone] init];
    copy.limitTimePeriods = [self.limitTimePeriods copyWithZone:zone];
    copy.limitTimeString = [self.limitTimeString copyWithZone:zone];
    copy.showTimePeriods = [self.showTimePeriods copyWithZone:zone];
    copy.requestTime = [self.requestTime copyWithZone:zone];
    copy.isAllowedTrade = self.isAllowedTrade;
    copy.versionString = [self.versionString copyWithZone:zone];
    copy.holidayList = [self.holidayList copyWithZone:zone];
    copy.isSignAgreement = self.isSignAgreement;
    copy.token = [self.token copyWithZone:zone];
    copy.profitShare = self.profitShare;
    copy.exchangeRate = self.exchangeRate;
    copy.tradeCountList = [self.tradeCountList copyWithZone:zone];
    copy.quitCloseList = [self.quitCloseList copyWithZone:zone];
    copy.quitGainList = [self.quitGainList copyWithZone:zone];
    copy.minFloatPoint = self.minFloatPoint;
    copy.contractValue = self.contractValue;
    copy.lossRatio = self.lossRatio;
    copy.serviceCharge = self.serviceCharge;
    copy.contractCode = [self.contractCode copyWithZone:zone];
    copy.isAgreementUpdatd = self.isAgreementUpdatd;
    copy.name = [self.name copyWithZone:zone];
    
    return copy;
}


@end


@implementation YZMHangSengConfigureModel

- (instancetype)init{
    
    self = [super init];
    if (self) {
        
        self.showTimePeriods = [self periodAnalyzeWithString:@"9:30,12:00|13:00,16:00"];
        self.moneySymbol = @"HK$";
        
    }
    return self;
    
}

- (NSString *)formatPrice:(double)price{
    return @(price).p0fString;
}

- (BOOL)isInHolidayWithDate:(NSDate *)date{
    
    for (NSDate *holiday in self.holidayList) {
        if ([date isSameDay:holiday]) {
            
            return YES;
        }
    }
    
    return NO;
}

- (BOOL)isInWeekendWithDate:(NSDate *)date{
    
    return [date isWeekend];
}

- (NSArray *)getCurrentDatePeriodList{
    
    return [self currentTimePeriod];
}

#define sYZMHangSengConfigureDataDefault  @"YZMHangSengConfigureDataDefault"

- (void)createEncoder{
    
    [self createEncoderWithIdentify:sYZMHangSengConfigureDataDefault];
}
+ (instancetype)createDecoder{
    
    return [self createDecoderWithIdentify:sYZMHangSengConfigureDataDefault];
}


@end

@implementation YZMAmericaConfigureModel

- (instancetype)init{
    
    self = [super init];
    if (self) {
        
        self.distanceForHour = 6;
        self.showTimePeriods = [self periodAnalyzeWithString:@"6:00,24:00|0:00,05:00"];
        self.moneySymbol = @"$";
        
    }
    return self;
}

- (BOOL)isInHolidayWithDate:(NSDate *)date{
    
    //计算与美交所的时间差
    if (self.limitTimePeriods.count == 0) {
        
        return NO;
    }
    
    for (NSDate *holiday in self.holidayList) {
        if ([[date dateBySubtractingHours:self.distanceForHour] isSameDay:holiday]) {
            
            return YES;
        }
    }
    
    return NO;
}

- (BOOL)isInWeekendWithDate:(NSDate *)date{
    
    if (self.limitTimePeriods.count == 0) {
        
        return NO;
    }
    
    return [[date dateBySubtractingHours:self.distanceForHour] isWeekend];
}


- (NSArray *)getCurrentDatePeriodList{
    
    //原油/黄金 计算显示的时间轴数据
    NSArray *array = [self currentTimePeriod];
    if (array.count == 0) {
        
        return nil;
    }
    
    DTTimePeriod *period =  array[0];
    NSDate *startDate;
    
    
    if ([self isInHolidayOrWeekendWithDate:[YZMSeverTimeService sharedService].severDate]) {
        
        //非交易日，显示最后4小时
        startDate = [period.EndDate dateBySubtractingHours:4];
        
    }else{
        
        //交易日
        NSInteger distance = ceil([[YZMSeverTimeService sharedService].severDate hoursFrom:period.StartDate]);
        
        if (distance <= 4) {
            
            startDate = period.StartDate;
            
            
        }else{
            
            NSInteger subDistance = ceil([period.EndDate hoursFrom:[YZMSeverTimeService sharedService].severDate]);
            NSInteger totalDistance = ceil([period.EndDate hoursFrom:period.EndDate]);
            
            if (subDistance <= 4) {
                
                startDate = [period.StartDate dateByAddingHours:totalDistance-4];
            }else{
                
                startDate = [period.StartDate dateByAddingHours:distance-4];
            }
            
        }
    }
    
    
    
    
    
    NSMutableArray *resultArray = [NSMutableArray array];
    for (int i=0; i<4; i++) {
        
        DTTimePeriod *dt = [DTTimePeriod timePeriodWithStartDate:[startDate dateByAddingHours:i] endDate:[startDate dateByAddingHours:1+i]];
        [resultArray addObject:dt];
    }
    
    return resultArray;
}

@end


@implementation YZMGoldConfigureModel

- (NSString *)formatPrice:(double)price{
    return @(price).p01fString;
}


#define sYZMGoldConfigureDataDefault  @"YZMGoldConfigureDataDefault"

- (void)createEncoder{
    
    [self createEncoderWithIdentify:sYZMGoldConfigureDataDefault];
}
+ (instancetype)createDecoder{
    
    return [self createDecoderWithIdentify:sYZMGoldConfigureDataDefault];
}

@end


@implementation YZMCrudeConfigureModel

- (NSString *)formatPrice:(double)price{
    return @(price).p02fString;
}


#define sYZMCrudeConfigureDataDefault  @"YZMCrudeConfigureDataDefault"

- (void)createEncoder{
    
    [self createEncoderWithIdentify:sYZMCrudeConfigureDataDefault];
}
+ (instancetype)createDecoder{
    
    return [self createDecoderWithIdentify:sYZMCrudeConfigureDataDefault];
}

@end




